// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor(){
        this.oldSprite = null;
    },
    properties: {
        
    },

    onCollisionEnter(other, self){
        if(!other.node.isCancel){
            if(self.node.children.length == 0){
                let sprite = self.node.getComponent(cc.Sprite)
                sprite.spriteFrame = other.node.getComponent(cc.Sprite).spriteFrame;
                other.node.capture = true;
                other.node.targetPos = self.node.pos;
                self.node.opacity = 0.8*255|0;
            }
        }
    },
    onCollisionExit: function (other, self) {
        if(!other.node.isCancel){
            if(self.node.children.length == 0){
                self.node.opacity = 255;
                let sprite = self.node.getComponent(cc.Sprite)
                sprite.spriteFrame = this.oldSprite;
                other.node.capture = false;
                other.node.targetPos = null;
            }
        }
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.oldSprite = this.node.getComponent(cc.Sprite).spriteFrame;
        this.node.on('reset', ()=>{
            this.node.getComponent(cc.Sprite).spriteFrame = this.oldSprite;
            this.node.opacity = 255;
        })

    },

    start () {

    },

    // update (dt) {},
});
